package com.lovo.roomProject.system.dao.impl;

import com.lovo.roomProject.dao.BaseDAO;
import com.lovo.roomProject.pojo.CutPageBean;
import com.lovo.roomProject.pojo.UserDO;
import com.lovo.roomProject.system.dao.UserDAO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl extends BaseDAO implements UserDAO {


    @Override
    public List listSystem() {
        this.getConnection();
        String sql = "select *from t_system";
        List list = new ArrayList();
        try {
            statement = con.prepareStatement(sql);
            rs = statement.executeQuery();
            while (rs.next()) {
                UserDO userDO = new UserDO();
                userDO.setId(rs.getInt("sy_id"));
                userDO.setName(rs.getString("sy_name"));
                userDO.setPwd(rs.getString("sy_pwd"));
                userDO.setRealName(rs.getString("sy_real_name"));
                userDO.setGrade(rs.getString("sy_grade"));

                list.add(userDO);


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


        return list;
    }

    @Override
    public CutPageBean listUserByPage(int current) {
        return null;
    }


    /**
     * 通过管理员ID查询
     *
     * @param id
     * @return
     */
    @Override
    public UserDO getUserById(int id) {
        this.getConnection();
        String sql = "select * from t_system where sy_id=?";
        UserDO userDO = null;
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("sy_id"));
                userDO.setName(rs.getString("sy_name"));
                userDO.setPwd(rs.getString("sy_pwd"));
                userDO.setRealName(rs.getString("sy_real_name"));
                userDO.setGrade(rs.getString("sy_grade"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {

        }
        return userDO;
    }

    /**
     * 登录验证
     *
     * @param userName 账户
     * @param password 密码
     * @return
     */
    @Override
    public UserDO validateLogon(String userName, String password) {
        this.getConnection();
        String sql = "select * from t_system where sy_name=? and sy_pwd=?";
        UserDO userDO = null;
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, userName);
            statement.setObject(2, password);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getInt("sy_id"));
                userDO.setName(rs.getString("sy_name"));
                userDO.setPwd(rs.getString("sy_pwd"));
                userDO.setRealName(rs.getString("sy_real_name"));
                userDO.setGrade(rs.getString("sy_grade"));
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }
        return userDO;
    }


    /**
     * 管理员添加
     *
     * @param userDO
     */
    @Override
    public void insert(UserDO userDO) {
        this.getConnection();
        String sql = "insert into t_system(sy_name,sy_real_name,sy_grade) " +
                "value (?,?,?)";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO.getName());
            statement.setObject(2, userDO.getRealName());
            statement.setObject(3, userDO.getGrade());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }

    }

    /**
     * 通过ID删除管理员
     *
     * @param id
     */
    @Override
    public void delete(int id) {
        this.getConnection();
        String sql = "delete from t_system where sy_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }

    }

    @Override
    public void update(UserDO userDO) {
        this.getConnection();
        String sql = "update t_system set sy_pwd=?,sy_grade=? where sy_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO.getPwd());
            statement.setObject(2, userDO.getGrade());
            statement.setObject(3, userDO.getId());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }


    }

    /**
     * 通过管理用户名查找
     *
     * @param userName
     * @return
     */
    @Override
    public UserDO getUserByName(String userName) {
        this.getConnection();
        String sql = "select * from t_system where sy_name=?";
        UserDO userDO = null;
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, userName);
            rs = statement.executeQuery();
            if (rs.next()) {
                userDO = new UserDO();
//                userDO.setId(rs.getInt("sy_id"));
                userDO.setName(rs.getString("sy_name"));
//                userDO.setPwd(rs.getString("sy_pwd"));
//                userDO.setRealName(rs.getString("sy_real_name"));

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {

        }

        return userDO;

    }


}
